﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using TSVNAddin.Config;

namespace TSVNAddin.TSVN.Test
{
	[TestFixture]
	public class TortoiseProcExecutorFixture
	{
		private const string _FileName = @"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe";

		[Test]
		public void ExecCommandWithoutArguments()
		{
			var configStorage = MockRepository.GenerateStub<ConfigStorage>((XmlFileStorage)null);
			configStorage.Expect(cs => cs.LoadAddinConfig()).Return(new AddinConfig() { ProcPath = _FileName });

			TortoiseProcCommandType tortoiseCommandType = TortoiseProcCommandType.About;
			ProcessStarter processStarter = MockRepository.GenerateMock<ProcessStarter>();
			string expectedArguments = string.Format("/command:{0}", tortoiseCommandType);
			processStarter.Expect(x => x.Start(_FileName, expectedArguments));

			TortoiseProcExecutor executor = new TortoiseProcExecutor(processStarter, configStorage);
			executor.Exec(tortoiseCommandType);
			
			processStarter.VerifyAllExpectations();
		}

		[Test]
		public void ExecCommandWithPath()
		{
			var configStorage = MockRepository.GenerateStub<ConfigStorage>((XmlFileStorage)null);
			configStorage.Expect(cs => cs.LoadAddinConfig()).Return(new AddinConfig() { ProcPath = _FileName });

			string repositoryPath = @"c:\svn\myproject";
			TortoiseProcCommandType tortoiseCommandType = TortoiseProcCommandType.Update;

			ProcessStarter proceesStarter = MockRepository.GenerateMock<ProcessStarter>();
			string expectedArguments = string.Format("/command:{0} /path:\"{1}\"", tortoiseCommandType.ToString().ToLower(), repositoryPath);
			proceesStarter.Expect(x => x.Start(_FileName, expectedArguments));

			TortoiseProcExecutor executor = new TortoiseProcExecutor(proceesStarter, configStorage);
			executor.Exec(TortoiseProcCommandType.Update, repositoryPath);

			proceesStarter.VerifyAllExpectations();
		}

		[Test]
		public void ExecCommandWithPathAndArguments()
		{
			var configStorage = MockRepository.GenerateStub<ConfigStorage>((XmlFileStorage)null);
			configStorage.Expect(cs => cs.LoadAddinConfig()).Return(new AddinConfig() { ProcPath = _FileName });

			string repositoryPath = @"c:\svn\myproject";
			TortoiseProcCommandType tortoiseCommandType = TortoiseProcCommandType.Update;
			TortoiseProcArgumentType argumentType = TortoiseProcArgumentType.rev;

			ProcessStarter processStarter = MockRepository.GenerateMock<ProcessStarter>();
			string expectedArguments = string.Format("/command:{0} /path:\"{1}\" /{2}", tortoiseCommandType.ToString().ToLower(), repositoryPath, argumentType);
			processStarter.Expect(x => x.Start(_FileName, expectedArguments));

			TortoiseProcExecutor executor = new TortoiseProcExecutor(processStarter, configStorage);
			executor.Exec(TortoiseProcCommandType.Update, repositoryPath, argumentType);

			processStarter.VerifyAllExpectations();
		}
	}
}
